package com.bitin.adorn.core.security.exception;

import com.bitin.adorn.common.payload.ApiResultCode;
import com.bitin.adorn.common.util.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Spring Security 访问异常处理器
 *
 * @author Mr.Lu
 * @since 2024/12/23 17:01
 */
@Component
public class Auth2AccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        ResponseUtils.writeErrorMessage(response, ApiResultCode.ACCESS_UNAUTHORIZED);
    }
}
